package com.id.supaclean.ui.junk

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.id.supaclean.utils.Garbage
import com.id.supaclean.utils.GarbageManager
import com.id.supaclean.utils.WrapperGarbage
import com.id.supaclean.utils.group
import kotlinx.coroutines.flow.toList

/**
 * Created by YiGo on 2022/6/16.
 */
class JunkViewModel : ViewModel() {

    val data = mutableListOf<WrapperGarbage>()

    val preCleanGarbage = mutableSetOf<Garbage>()

    suspend fun initData() {
        data.clear()
        preCleanGarbage.clear()
        val garbageGroup =
            GarbageManager.scan().toList().group().filter { it.garbageList.isNotEmpty() }
        val itemList = mutableListOf<WrapperGarbage.Item>()
        garbageGroup.forEach { it ->
            it.garbageList.forEach {
                itemList.add(WrapperGarbage.Item(it))
            }
            val newList = mutableListOf<WrapperGarbage.Item>()
            newList.addAll(itemList)
            data.add(WrapperGarbage.Group(it.title, newList))
            itemList.clear()
        }
    }

    val currentPreGarbageNum: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }

    val currentPreGarbageUnit: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }

    fun countGarbage(preGarbageList: List<Garbage>): Long {
        var num = 0L
        preGarbageList.forEach {
            num += it.file.length()
        }
        return num
    }


}